package cn.dj.org.service.impl;

import cn.dj.basic.service.impl.BaseServiceImpl;
import cn.dj.basic.util.PageList;
import cn.dj.org.domain.Department;
import cn.dj.org.mapper.DepartmentMapper;
import cn.dj.org.query.DepartmentQuery;
import cn.dj.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：部门
 */
//前提：扫描包路径
@Service //1.将当前类交给Spring管理 = 2.实例化对象注解 = 3.创建对象的注解[这样Spring容器中有该类的对象，才能在其他地方注入该对象]
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    //注入
    @Autowired
    private DepartmentMapper departmentMapper;
/*
    //增删改用 @Transactional 默认的readOnly = false,propagation = Propagation.REQUIRED
    @Transactional
    @Override
    public void save(Department department) {
        departmentMapper.save(department);
    }
    @Transactional
    @Override
    public void update(Department department) {
        departmentMapper.update(department);
    }
    @Transactional
    @Override
    public void del(Long id) {
        departmentMapper.del(id);
    }

    @Override
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }

    @Override
    public Department loadById(Long id) {
        return departmentMapper.loadById(id);
    }

    @Override
    public PageList<Department> queryPage(DepartmentQuery departmentQuery) {
       //1.查询总数量
        Integer totals = departmentMapper.queryCount(departmentQuery);
        if(totals < 1){//没有查询到数据的情况
            return new PageList<>();
        }
        //2.查询当前页的分页数据
        List<Department> departments = departmentMapper.queryData(departmentQuery);
        return new PageList<>(totals,departments);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        departmentMapper.patchDelete(ids);
    }*/

    @Override
    public List<Department> deptTree() {
        List<Department> departments = departmentMapper.loadAll();
        //1.将所有部门信息保存在map中，方便根据id去获取部门信息，就不用查数据库了
        Map<Long,Department> map = new HashMap<>();
        for (Department department : departments) {
            map.put(department.getId(),department);
        }
        List<Department> deptTree = new ArrayList<>();
        for (Department department : departments) {
            if(department.getParent_id()==null){//顶级部门
                //2.遍历所有的部门，如果是顶级部门[parent_id==null]，加到部门树
                deptTree.add(department);
            }else{
                //3.如果不是顶级部门【parent_id!=null]】，找到自己的父部门，将自己加到父部门的children集合
                //获取父部门的id - 是为了获取父部门对象 - 获取父部门的children集合
                Long parentId = department.getParent_id();
                //根据id获取父部门对象
                Department parentDepartment = map.get(parentId);
                //防止父部门数据的删除引起的空指针异常
                if(parentDepartment!=null){//为null不能调用方法，会报错
                parentDepartment.getChildren().add(department);
                }
            }

        }
        return deptTree;
    }
}
